package com.ruoyi.wms.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.id.IdGenarator;
import com.ruoyi.wms.domain.WmsInventory;
import com.ruoyi.wms.domain.WmsReceiptOrderDetail;
import com.ruoyi.wms.mapper.WmsInventoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.wms.domain.WmsShipmentOrderDetail;
import com.ruoyi.wms.mapper.WmsShipmentOrderMapper;
import com.ruoyi.wms.domain.WmsShipmentOrder;
import com.ruoyi.wms.service.IWmsShipmentOrderService;

/**
 * 出库单Service业务层处理
 *
 * @author zmm
 * @date 2023-01-30
 */
@Service
public class WmsShipmentOrderServiceImpl implements IWmsShipmentOrderService
{
    @Autowired
    private WmsShipmentOrderMapper wmsShipmentOrderMapper;
    @Autowired
    private WmsInventoryMapper inventoryMapper;

    /**
     * 查询出库单
     *
     * @param id 出库单主键
     * @return 出库单
     */
    @Override
    public WmsShipmentOrder selectWmsShipmentOrderById(Long id)
    {
        return wmsShipmentOrderMapper.selectWmsShipmentOrderById(id);
    }

    /**
     * 查询出库单列表
     *
     * @param wmsShipmentOrder 出库单
     * @return 出库单
     */
    @Override
    public List<WmsShipmentOrder> selectWmsShipmentOrderList(WmsShipmentOrder wmsShipmentOrder)
    {
        return wmsShipmentOrderMapper.selectWmsShipmentOrderList(wmsShipmentOrder);
    }

    /**
     * 新增出库单
     *
     * @param wmsShipmentOrder 出库单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsShipmentOrder(WmsShipmentOrder wmsShipmentOrder)
    {
        List<WmsShipmentOrderDetail> wmsShipmentOrderDetailList = wmsShipmentOrder.getWmsShipmentOrderDetailList();
        if (CollectionUtil.isEmpty(wmsShipmentOrderDetailList)){
            throw  new RuntimeException("请填写物料明细");
        }
        //查库存
        for (int i = 0; i < wmsShipmentOrderDetailList.size(); i++) {
            BigDecimal quantity=BigDecimal.ZERO;
            WmsInventory wmsInventory = new WmsInventory();
            wmsInventory.setAreaId(wmsShipmentOrderDetailList.get(i).getAreaId());
            wmsInventory.setItemId(wmsShipmentOrderDetailList.get(i).getItemId());
            wmsInventory.setWarehouseId(wmsShipmentOrderDetailList.get(i).getWarehouseId());
            wmsInventory.setRackId(wmsShipmentOrderDetailList.get(i).getRackId());
            List<WmsInventory> wmsInventories = inventoryMapper.selectWmsInventoryList(wmsInventory);
            if (CollectionUtil.isEmpty(wmsInventories)){
                throw new RuntimeException("您选择的货架下第"+(i+1)+"个物料无库存,请重新选择");
            }else {
                wmsInventory=wmsInventories.get(0);
                quantity=wmsInventory.getQuantity();
                //判断库存数量
                if (quantity.compareTo(wmsShipmentOrderDetailList.get(i).getRealQuantity())<0){

                    throw new RuntimeException("您选择的货架下第"+(i+1)+"个物料库存小于出库数量,请重新选择");
                }
                wmsInventory.setQuantity(quantity.subtract(wmsShipmentOrderDetailList.get(i).getRealQuantity()));
                inventoryMapper.updateWmsInventory(wmsInventory);
            }

        }




        wmsShipmentOrder.setCreateTime(DateUtils.getNowDate());
        int rows = wmsShipmentOrderMapper.insertWmsShipmentOrder(wmsShipmentOrder);
        insertWmsShipmentOrderDetail(wmsShipmentOrder);
        return rows;
    }

    /**
     * 修改出库单
     *
     * @param wmsShipmentOrder 出库单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsShipmentOrder(WmsShipmentOrder wmsShipmentOrder)
    {
        wmsShipmentOrder.setUpdateTime(DateUtils.getNowDate());
        wmsShipmentOrderMapper.deleteWmsShipmentOrderDetailByShipmentOrderId(wmsShipmentOrder.getId());
        insertWmsShipmentOrderDetail(wmsShipmentOrder);
        return wmsShipmentOrderMapper.updateWmsShipmentOrder(wmsShipmentOrder);
    }

    /**
     * 批量删除出库单
     *
     * @param ids 需要删除的出库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsShipmentOrderByIds(Long[] ids)


    {
        for (Long id : ids) {
            //查询明细
            WmsShipmentOrder wmsShipmentOrder = wmsShipmentOrderMapper.selectWmsShipmentOrderById(id);
            List<WmsShipmentOrderDetail> wmsShipmentOrderDetailList = wmsShipmentOrder.getWmsShipmentOrderDetailList();
            if (CollectionUtil.isEmpty(wmsShipmentOrderDetailList)) {
                throw new RuntimeException("请填写物料明细");
            }
            for (WmsShipmentOrderDetail wmsReceiptOrderDetail : wmsShipmentOrderDetailList) {
                BigDecimal quantity = BigDecimal.ZERO;
                BigDecimal realQuantity = wmsReceiptOrderDetail.getRealQuantity();
                //库存操作 先查询 同仓库 库区 货架 物料 有没有库存
                WmsInventory wmsInventory = new WmsInventory();
                wmsInventory.setAreaId(wmsReceiptOrderDetail.getAreaId());
                wmsInventory.setItemId(wmsReceiptOrderDetail.getItemId());
                wmsInventory.setWarehouseId(wmsReceiptOrderDetail.getWarehouseId());
                wmsInventory.setRackId(wmsReceiptOrderDetail.getRackId());
                List<WmsInventory> wmsInventories = inventoryMapper.selectWmsInventoryList(wmsInventory);
                if (CollectionUtil.isEmpty(wmsInventories)) {
                    inventoryMapper.insertWmsInventory(wmsInventory);
                } else {
                    wmsInventory = wmsInventories.get(0);
                    quantity = wmsInventory.getQuantity();
                }
                wmsInventory.setQuantity(quantity.add(realQuantity));
                inventoryMapper.updateWmsInventory(wmsInventory);
            }


        }


        wmsShipmentOrderMapper.deleteWmsShipmentOrderDetailByShipmentOrderIds(ids);
        return wmsShipmentOrderMapper.deleteWmsShipmentOrderByIds(ids);
    }

    /**
     * 删除出库单信息
     *
     * @param id 出库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsShipmentOrderById(Long id)
    {



        wmsShipmentOrderMapper.deleteWmsShipmentOrderDetailByShipmentOrderId(id);
        return wmsShipmentOrderMapper.deleteWmsShipmentOrderById(id);
    }

    /**
     * 新增出库单详情信息
     *
     * @param wmsShipmentOrder 出库单对象
     */
    public void insertWmsShipmentOrderDetail(WmsShipmentOrder wmsShipmentOrder)
    {
        List<WmsShipmentOrderDetail> wmsShipmentOrderDetailList = wmsShipmentOrder.getWmsShipmentOrderDetailList();
        Long id = wmsShipmentOrder.getId();
        if (StringUtils.isNotNull(wmsShipmentOrderDetailList))
        {
            List<WmsShipmentOrderDetail> list = new ArrayList<WmsShipmentOrderDetail>();
            for (WmsShipmentOrderDetail wmsShipmentOrderDetail : wmsShipmentOrderDetailList)
            {
                wmsShipmentOrderDetail.setId(IdGenarator.nextId());
                wmsShipmentOrderDetail.setShipmentOrderId(id);
                wmsShipmentOrderDetail.setCreateTime(wmsShipmentOrder.getCreateTime());
                wmsShipmentOrderDetail.setCreateBy(wmsShipmentOrder.getCreateBy());
                wmsShipmentOrderDetail.setDelFlag(0);
                list.add(wmsShipmentOrderDetail);
            }
            if (list.size() > 0)
            {
                wmsShipmentOrderMapper.batchWmsShipmentOrderDetail(list);
            }
        }
    }
}
